package com.sinosoft.perfect.system._.service;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpSession;

import com.sinosoft.perfect.system._.dao.ILoginUserResourceDao;
import com.sinosoft.perfect.system._.po.TResourceSystem;
import com.sinosoft.perfect.system._.vo.LoginUser;
import com.sinosoft.perfect.system._.vo.ResourceURLPower;

public class LoginUserResourceServiceImpl implements ILoginUserResourceService {

	private ILoginUserResourceDao  iloginUserResourceDao;
	
	public void setIloginUserResourceDao(ILoginUserResourceDao iloginUserResourceDao) {
		this.iloginUserResourceDao = iloginUserResourceDao;
	}

	@Override
	public List<TResourceSystem> initLoginUserResourceList(HttpSession session)
			throws SQLException {
		LoginUser loginUser = (LoginUser)session.getAttribute("user");
		String userId = loginUser.getUserMainId();	
		String userType = loginUser.getUserType();
		List<TResourceSystem> resourceSystems = null;
		if("0".equals(userType)){
			//userSuperAdmin
			 resourceSystems = iloginUserResourceDao.initLoginUserResourceListAdmin(userId);
		}else{
			//userNormal
			 resourceSystems = iloginUserResourceDao.initLoginUserResourceList(userId);
		}
		return resourceSystems;
	}

	@Override
	public ResourceURLPower getResourceURLPower(String userId,
			String resourceId,String userType) throws SQLException {
		ResourceURLPower resourceURLPower = this.getNewResourceURLPower(userId, resourceId);
		List<ResourceURLPower> resourceURLPowers = null;
		resourceURLPowers = iloginUserResourceDao.getResourceURLPowers(resourceURLPower);
		for(int i=0;i<resourceURLPowers.size();i++){
			ResourceURLPower rp = resourceURLPowers.get(i);
			int powerInsert = rp.getPowerInsert();
			int powerDelete = rp.getPowerDelete();
			int powerUpdate = rp.getPowerUpdate();
			int operationLevel = rp.getOperationLevel();
			int operationAll = rp.getOperationAll();
			if(powerInsert != 0 && resourceURLPower.getPowerInsert() == 0){
				resourceURLPower.setPowerInsert(powerInsert);
			}
			if(powerDelete != 0 && resourceURLPower.getPowerDelete() == 0){
				resourceURLPower.setPowerDelete(powerDelete);
			}
			if(powerUpdate != 0 && resourceURLPower.getPowerUpdate() == 0){
				resourceURLPower.setPowerUpdate(powerUpdate);
			}
			if(operationLevel != 0 && resourceURLPower.getOperationLevel() == 0){
				resourceURLPower.setOperationLevel(operationLevel);
			}
			if(operationAll != 0 && resourceURLPower.getOperationAll() == 0){
				resourceURLPower.setOperationAll(operationAll);
			}
			if(rp.getResourceId().equals(resourceId) && resourceURLPower.getResourceURL() == null){
				resourceURLPower.setResourceURL(rp.getResourceURL());
			}
		}
		if("0".equals(userType)){
			//userSuperAdmin
			return this.setResourceURLPowerAdmin(resourceURLPower);
		}else {
			//userNormal
			return resourceURLPower;
		}
	}

	private ResourceURLPower getNewResourceURLPower(String userId,String resourceId){
		ResourceURLPower resourceURLPower = new ResourceURLPower();
		resourceURLPower.setUserId(userId);
		resourceURLPower.setResourceId(resourceId);
		resourceURLPower.setPowerInsert(0);
		resourceURLPower.setPowerDelete(0);
		resourceURLPower.setPowerUpdate(0);
		resourceURLPower.setOperationLevel(0);
		resourceURLPower.setOperationAll(0);
		return resourceURLPower;
	}
	
	private ResourceURLPower setResourceURLPowerAdmin(ResourceURLPower resourceURLPowerParam){
		ResourceURLPower resourceURLPower = resourceURLPowerParam;
		resourceURLPower.setPowerInsert(1);
		resourceURLPower.setPowerDelete(1);
		resourceURLPower.setPowerUpdate(1);
		resourceURLPower.setOperationLevel(0);
		resourceURLPower.setOperationAll(1);
		return resourceURLPower;
	}
	
}
